<?php
class PageModel extends Model {

	function __construct()
		{
			parent::Model();
			$this->load->library(array('treelib', 'pagelib', 'imagelib'));
		}

	public  function action($type, $id)
		{
			switch ($type) {
				case "add":
					$this->addPage($id);
					break;
				case "edit":
				   $this->editPage($id);
					break;
				case "delete":
					$this->deletePage($id);
					break;
				case "delVideo":
					$this->deleteVideo($id);
					break;
				}
		}
	
	private function addPage($id)
		{
			$pages = new Page();

			$pages->name = $this->input->post('name',true);
			$pages->description = $this->input->post('description',true);
			$pages->parent_id = $this->input->post('parent_id',true);
			$pages->level = $this->pagelib->level($this->input->post('parent_id',true));
			$pages->orders = $this->pagelib->order($this->input->post('order',true));
			$pages->slug = $this->input->post('slug',true);
			$pages->link = $this->pagelib->link($this->input->post('parent_id',true)).$this->input->post('slug',true).'/';
			
			$pages->save();

			redirect('/admin/pages/edit/'.$pages->id);
		}
	
	private function editPage($id)
		{
			$page = $this->getPage($id);

			$page->name = $this->input->post('name',true);
			$page->description = $this->input->post('description',true);
			$page->link = $this->input->post('link',true);
			$page->slug = $this->input->post('slug',true);
			$page->css = implode(",", $this->input->post('css',true));
			$page->js = implode(",", $this->input->post('js',true));
			$page->meta_title = $this->input->post('meta_title',true);
			$page->meta_keywords = $this->input->post('meta_keywords',true);
			$page->meta_description = $this->input->post('meta_description',true);
			
			if ($_FILES['uservideo']['name'] != '') 
			{
				$video = $this->imagelib->uploadVideo('pages');
				$page->video = $video['file_name'];
			}

			$page->save();
			redirect('/admin/pages/edit/'.$page->id.'/');
		}
		
	private function deletePage($id)
		{
			$pages = $this->getPage($id);
			$pages->delete();
			echo "Страница ".$this->input->post('name',true)." удалена";
		}
		
	private function deleteVideo($id)
		{
			$pages = $this->getPage($id);
			if (isset($pages->video) && file_exists('res/img/pages/'.$pages->video)) {
				unlink('res/img/pages/'.$pages->video);
			}
			$pages->video = '';
			$pages->save();
		}

	function getPage($id, $array = false)
		{
			$page = Doctrine::getTable('Page')->find($id);
			if($page !== false) {
				if ($array) {
					return $page->toArray(true);
				}else{
					return $page;
				}
			}else{
				show_404('page');
			}
		}

	function getPageByLink($id, $array = false)
		{
			$page = Doctrine::getTable('Page')->findOneByLink($id);
			if($page !== false) {
				if ($array) {
					return $page->toArray(true);
				}else{
					return $page;
				}
			}else{
				show_404('page');
			}
		}
	
	function getPageByParent($id, $array = false)
		{
			$page = Doctrine::getTable('Page')->findByParent_id($id)->getLast();
			if($page !== false) {
				if ($array) {
					return $page->toArray(true);
				}else{
					return $page;
				}
			}else{
				show_404('page');
			}
		}

	function getTree($level = "0")
		{
			return $this->treelib->get($level, 'Page');
		}
}
?>